<include file="public@header"/>
</head>
<body>
<div class="wrap js-check-wrap">
	<ul class="nav nav-tabs">
		<li ><a href="{:url('AdminExpressTemplate/index')}">运费模板显示</a></li>
		<li class="active"><a href="{:url('AdminExpressTemplate/add')}">添加运费模板</a></li>
	</ul>
	<form method="post" class="form-horizontal js-ajax-form margin-top-20" action="{:url('AdminExpressTemplate/addPost')}">
		<div class="form-group">
			<label class="col-sm-2 control-label">排序:</label>
			<div class="col-md-6 col-sm-10">
				<input type="text" class="form-control" name="list_order" >
				<small style="color: red">* 越小越靠前,默认10000</small>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-2 control-label"><span class="form-required">*</span>运费模板名称:</label>
			<div class="col-md-6 col-sm-10">
				<input type="text" class="form-control" name="name" required>
			</div>
		</div>
		<!--<div class="form-group">
			<label class="col-sm-2 control-label"><span class="form-required">*</span>计费类型:</label>
			<div class="col-md-6 col-sm-10">
				<select class="form-control" name="charging_type" id="input-parent">
					<option value="0">按重量计费</option>
					<option value="1">按件计费</option>
				</select>
			</div>
		</div>-->

		<div class="form-group">
			<label class="col-sm-2 control-label"><span class="form-required">*</span>快递公司:</label>
			<div class="col-md-6 col-sm-10">
				<select class="form-control" name="expreess_type" id="input-expreess_type">
					<foreach name="express_type" id="ex_type">
						<option value="{$ex_type['id']}">{$ex_type['name']}</option>
					</foreach>
				</select>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-2 control-label"><span class="form-required"></span>配送区域:</label>
			<div class="col-md-6 col-sm-10">
				<table class="table  table-bordered table-list">
					<thead>
					<tr>
						<th width="50%">配送到</th>
						<th width="10%">首重</th>
						<th width="10%">首费</th>
						<th width="10%">续重</th>
						<th width="10%">续费</th>
						<th width="10%">管理</th>
					</tr>
					</thead>
					<tbody>
						<tr>
							<td>全国【默认运费】</td>
							<td>
								<input type="number" value="1000" name="ykg"></td>
							<td>
								<input type="number" value="0" name="ymoney"></td>
							<td>
								<input type="number" value="1000" name="addkg"></td>
							<td>
								<input type="number" value="0" name="addmoney"></td>
							<td></td>
						</tr>

					</tbody>
				</table>
				<br>
				<div class="btn btn-default" onclick="openExpressCity()">
					<i class="fa fa-plus"></i>
					新增配送区域
				</div>
				<!--记录已选择的城市-->
				<input type="text" value="" id="js-categories-id-input">


			</div>
		</div>
		<div class="col-sm-offset-2 col-sm-10">
			<button type="submit" class="btn btn-primary js-ajax-submit">{:lang("SAVE")}</button>
			<a class="btn btn-default" href="__URL__">返回</a>
		</div>
	</form>

</div>
<script src="__STATIC__/js/admin.js"></script>
<script>
	function removeTr(e) {
		//去除掉#js-categories-id-input 下的id
		var ids=$('#js-categories-id-input').val();
		//字符串拆分数组
		var ids_arr=ids.split(',');

		var remove_ids=$(e).data('ids').toString();
		var remove_ids_arr=remove_ids.split(',');

		//计算数组差集
		var new_ids=arr_dive(ids_arr,remove_ids_arr);
		//console.log(new_ids);
		$('#js-categories-id-input').val(new_ids.join(','));
		//console.log(ids);
		//console.log(remove_ids);
		$(e).parent().parent().remove();
	}
	function openExpressCity() {
		var selectedCategoriesId = $('#js-categories-id-input').val();
		openIframeLayer("{:url('AdminExpressTemplate/getRegion')}?cities=" + selectedCategoriesId, '请选择配送区域', {
			area: ['700px', '400px'],
			btn: ['确定', '取消'],
			yes: function (index, layero) {

				var iframeWin          = window[layero.find('iframe')[0]['name']];
				var selectedCategories = iframeWin.confirm();
				console.log(selectedCategories);
				if (selectedCategories.selectedCategoriesId.length == 0) {
					layer.msg('请选择配送区域');
					return;
				}
				//增加一行
				var str='<tr>' +
							'<td>';
					str +=selectedCategories.selectedCategoriesName.join(' ');
					str +='</td>' +
							'<td><input type="number" value="1000" name="new_ykg[]"></td>' +
							'<td><input type="number" value="0" name="new_ymoney[]"></td>' +
							'<td><input type="number" value="1000" name="new_addkg[]"></td>' +
							'<td><input type="number" value="0" name="new_addmoney[]"></td>' +
							'<td><a href="javascript:void(0)" onclick="removeTr(this)" data-ids="';
				    str +=selectedCategories.selectedCategoriesId.join(',');
				    str +='"><i class="fa fa-times"></i></a><input type="hidden" value="' ;
				    str +=selectedCategories.selectedCategoriesId.join(',');
					str +='" name="new_city_ids[]"></td>' +
						'</tr>';
				$('tbody').append(str);
				//追加选中ids
				var ids=$('#js-categories-id-input').val();
				var add_ids=selectedCategories.selectedCategoriesId.join(',');
				if(ids == ''){
					$('#js-categories-id-input').val(add_ids);
				}else{
					ids +=',' +add_ids;
					$('#js-categories-id-input').val(ids);
				}

				//$('#js-categories-id-input').val(selectedCategories.selectedCategoriesId.join(','));
				//$('#js-categories-name-input').val(selectedCategories.selectedCategoriesName.join(' '));
				//console.log(layer.getFrameIndex(index));
				layer.close(index); //如果设定了yes回调，需进行手工关闭
			}
		});
	}
	//第一个数组大 第二个数组小
	function arr_dive(aArr,bArr){
		var a=aArr;
		var b=bArr;
		var c = [];
		var tmp = a.concat(b);
		var o = {};
		for (var i = 0; i < tmp.length; i ++) (tmp[i] in o) ? o[tmp[i]] ++ : o[tmp[i]] = 1;
		for (x in o) if (o[x] == 1) c.push(x);
		return c;
	}

</script>
</body>
</html>